# coding=utf-8
# h
import smtplib
from email.mime.text import MIMEText


def send_email():
    try:
        host= 'smtp.qq.com'
        port=25
        sender='2509570379@qq.com'
        sender_alias='yjw02@qq.com'
        password='yin05033512'
        receiver='571716604@qq.com'
        body='<h1>Python自动发送的第一封邮件</h1>'
        msg=MIMEText(body,'html')
        msg['subject']='效率优先，我用Python'
        msg['from']=sender_alias
        msg['to']=receiver

        s=smtplib.SMTP(host,port)
        s.login(sender,password)
        s.sendmail(sender,receiver,msg.as_string())
    except Exception as  e:
        print(e)


# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header


class Mail:
    def __init__(self):
        # 第三方 SMTP 服务

        self.mail_host = "smtp.qq.com"  # 设置服务器:这个是qq邮箱服务器，直接复制就可以
        self.mail_pass = "bjbpyfxqivkaebcf"  # 刚才我们获取的授权码
        self.sender = '2509570379@qq.com'  # 你的邮箱地址
        # self.receivers = ['yjwforrest@gmail.com','571716604@qq.com','229717148@qq.com']  # 收件人的邮箱地址，可设置为你的QQ邮箱或者其他邮箱，可多个
        self.receivers = ['yjwforrest@gmail.com','571716604@qq.com']  # 收件人的邮箱地址，可设置为你的QQ邮箱或者其他邮箱，可多个

    def send(self):
        # 发件内容
        content = '<h1>君住湘江头</h1><br/><h1>我住湘江尾</h1><br/><h1>日日思君不见君</h1><br/><h1>共饮湘江水</h1><br/><h2>来自Python的一封邮件</h2>'
        # message = MIMEText(content, 'plain', 'utf-8')
        message = MIMEText(content, 'html', 'utf-8')
        # 发件人名字，可自由填写
        message['From'] = Header("老公", 'utf-8')
        # 收件人名字，可自由填写
        message['To'] = Header("老婆", 'utf-8')

        subject = '来自老公使用Python的一封邮件'  # 发送的主题，可自由填写
        message['Subject'] = Header(subject, 'utf-8')
        try:
            smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)
            smtpObj.login(self.sender, self.mail_pass)
            smtpObj.sendmail(self.sender, self.receivers, message.as_string())
            smtpObj.quit()
            print('邮件发送成功')
        except smtplib.SMTPException as e:
            print('邮件发送失败')


if __name__ == '__main__':
    mail = Mail()
    mail.send()
